<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Subcribe</title>
    <link rel="stylesheet" href="../public/js/bootstrap/css/bootstrap.css">
    <script src="http://push-server-demos.vip.com:4000/socket.io/socket.io.js"></script>
    <script src="../public/js/jquery.min.js"></script>
    <script src="../public/js/bootstrap/js/bootstrap.min.js"></script>
    <style>
        body{
            padding-top: 100px;
        }
        .box{
            margin-right: 0;
            margin-left: 0;
            background-color: #fff;
            border-color: #333;
            border-width: 1px;
            border-radius: 4px 4px 0 0;
            -webkit-box-shadow: none;
            box-shadow: none;
            position: relative;
            padding: 25px 15px 15px;
            border-color: #ccc #ccc #ccc;
            border-style: solid;
        }
        .productList, .pUlBox{
            padding: 0px;
            margin: 0px;
            width: 800px;
            float:left;
            list-style: none;
        }
        .pUlBox{
            width : 750px;
        }
        .productList li,.pUlBox li{
            width: 70px; 
            margin-top : 10px; 
            margin-right : 5px;           
            float: left;
        }
        .msgBox{
            width: 800px;
        }
        .pBox{
            float : left;
        }
        .msgLists{
            list-style: none;
        }
    </style>
</head>
<body>
    <div class="container" style="">
        您好，<?php echo $_COOKIE['uid'];?> 
        <a href="?act=logout">[登出]</a>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <a class="btn btn-primary" target="_blank" href="manager.php">消息发布管理页面</a>
        <br/><br/>
        <ul class="productList">
            点击以下商品订阅：<br/><br/>
            <?php foreach($unSubProducts as $product){?>
                <li>
                    <a class="btn btn-default product" pid="<?php echo $product[0]?>" href="###">
                        <?php echo $product[1]?>
                    </a>
                </li>
            <?php }?>
        </ul>
    </div>
    <div class="container" style="margin-top:40px;">
        <div style="float:left;">
            您订阅的商品列表（点击取消）：<br/><br/>
            <div class="msgBox box pBox" style="float:left">
                <ul class="pUlBox">
                    <?php foreach($subProducts as $product){?>
                        <li>
                            <a class="btn btn-default product" pid="<?php echo $product[0]?>" href="###">
                                <?php echo $product[1]?>
                            </a>
                        </li>
                    <?php }?>
                </ul>
            </div>
            <div style="width: 800px;float:left;margin-top:10px;">
                 <button style="float:right" class="btn btn-default btn-success confirm">确认订阅</button>
            </div>
        </div>
        <div style="float:left;margin-top:20px;">
            消息列表：<br/><br/>
            <div class="msgBox box" style="float:left">
                <ul class="msgLists list-unstyled"></ul>
            </div>
        </div>
    </div>
</body>
<script>
    $(document).ready(function(){
        var params = <?php echo json_encode($params);?>;
        var products = <?php echo json_encode($products);?>;
        var uid    = '<?php echo $params["uid"];?>';
        var socket = io.connect('http://push-server-demos.vip.com:4000',{ query: $.param(params) });
        
        $('.productList').on('click','.product',function(){
            $(this).parents('li').appendTo($('.pUlBox'));
        });

        $('.pBox').on('click','.product',function(){
            $(this).parents('li').appendTo($('.productList'));
        });

        socket.on('message', function(msg){
            var htmlStr = '<li>' + products[msg.group][1] + ' 有货啦，赶快过来订购吧！</li>';
            $(htmlStr).appendTo($('.msgLists'));
        });

        $('.confirm').click(function(){
            var groups = [];
            $('.pBox').find('.product').each(function(i, item){
                groups.push($(item).attr('pid'));
            });
            if(groups.length > 0){
                $.ajax({
                    url : 'index.php',
                    type : 'POST',
                    data : { act : 'update_uid_group' , groups : groups},
                    dataType : 'JSON',
                    success : function(res){
                        if(res.code == 0){
                            alert('订阅成功！');
                        }else{
                            alert(res.msg);
                        }
                    }
                });
            }
        });
    });
</script>
</html>